package com.ruoyi.kong.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.kong.entity.KongConsumer;
import com.ruoyi.kong.entity.KongEntity;
import com.ruoyi.kong.entity.KongPlugin;
import com.ruoyi.kong.service.IKongConsumerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/kong/consumer")
public class KongConsumerController extends BaseController {

    @Autowired
    private IKongConsumerService kongConsumerService;

    @GetMapping("/list")
    public TableDataInfo getConsumers(){
        startPage();
        KongEntity<KongConsumer> consumers = kongConsumerService.consumers();
        return getDataTable(consumers.getData());
    }

    @PostMapping("/add")
    public AjaxResult addConsumer(@RequestBody  KongConsumer consumer){
        KongConsumer kongConsumer = kongConsumerService.addConsumer(consumer);
        return AjaxResult.success(kongConsumer);
    }

    @PostMapping("/delete/{id}")
    public AjaxResult deleteConsumer(@PathVariable String id){
        kongConsumerService.deleteConsumer(id);
        return success();
    }

    @PostMapping("/update/{id}")
    public AjaxResult updateConsumer(@PathVariable String id, @RequestBody KongConsumer consumer){
        KongConsumer kongConsumer = kongConsumerService.updateConsumer(id, consumer);
        return AjaxResult.success(kongConsumer);
    }

    @GetMapping("/plugins/{id}")
    public TableDataInfo consumerPlugins(@PathVariable String id){
        startPage();
        KongEntity<KongPlugin> kongPlugins = kongConsumerService.consumerPlugins(id);
        return getDataTable(kongPlugins.getData());
    }


    @GetMapping("/credential/{id}/{name}")
    public AjaxResult consumerCredential(@PathVariable String id,@PathVariable String name){
        KongEntity<Map> mapKongEntity = kongConsumerService.consumerCredentialByName(id, name);
        return AjaxResult.success(mapKongEntity);
    }

    @PostMapping("/credential/add/{id}/{name}")
    public AjaxResult addConsumerCredential(@PathVariable("id") String id,@PathVariable("name") String name,@RequestBody Map entity){
        KongEntity<Map> mapKongEntity = kongConsumerService.addConsumerCredential(id, name, entity);
        return AjaxResult.success(mapKongEntity);
    }

    @PostMapping("/credential/delete/{id}/{name}/{entityId}")
    public AjaxResult deleteConsumerCredential (@PathVariable("id") String id,
                                                @PathVariable("name") String name,
                                                @PathVariable("entityId") String entityId){

        kongConsumerService.deleteConsumerCredential(id,name,entityId);
        return  success();
    }


}
